package com.nis.bcreceiver;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述:
 * 创建: zhangyh
 * 时间: 2018/9/13
 */

public class MyAdapter extends BaseAdapter {
    Activity activity;
    List<MsgBean> dataList = new ArrayList<>();

    public MyAdapter(Activity activity) {
        this.activity = activity;
    }

    public void setData(List<MsgBean> addData, boolean isFirst) {
        if (isFirst) {
            dataList.clear();
            if (addData != null) {
                dataList.addAll(addData);
            }
        } else {
            if (addData != null) {
                dataList.addAll(addData);
            }
        }
        notifyDataSetChanged();
    }
    public List<MsgBean> getDataList(){
        return dataList;
    }

    @Override
    public int getCount() {
        return dataList != null ? dataList.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(activity).inflate(R.layout.item_show, null);
            holder = new ViewHolder();
            holder.tvWhen = view.findViewById(R.id.time);
            holder.tvFrom = view.findViewById(R.id.from);
            holder.tvContent = view.findViewById(R.id.content);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MsgBean bean = dataList.get(i);
        holder.tvWhen.setText(bean.when + "");
        holder.tvFrom.setText(bean.from + "");
        holder.tvContent.setText(bean.content + "");
        return view;
    }

    class ViewHolder {
        TextView tvWhen, tvFrom, tvContent;
    }
}
